package com.ftg.learn.pattern;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author kangjx
 * @date 2022/11/28
 */
public class KjxMapperProxy {

    /**
     * 我想为接口Studentmapper创建一个实现类
     */
    public static StudentMapper getMapper(){
        //StudentMapper.class 就是我们的目标类
        // KjxMapperProxy - proxy生成
        Object object = Proxy.newProxyInstance(StudentMapper.class.getClassLoader(), new Class[]{
                StudentMapper.class
        }, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                KjxSelect kjxSelect = method.getAnnotation(KjxSelect.class);
                List<Map<String,Object>> list = new ArrayList<>();
                if(Objects.nonNull(kjxSelect)){
                    String sql = kjxSelect.value();
                    System.out.println(sql);
                    JdbcUtil jdbcUtil = new JdbcUtil();
                    list = jdbcUtil.query(sql);
                }
                System.out.println(proxy.getClass().getName());
                System.out.println(method.getName());
                System.out.println(args);
                return list;
            }
        });
        return (StudentMapper) object;
    }

    public static void main(String[] args) {
        StudentMapper studentMapper = KjxMapperProxy.getMapper();
        List<Map<String,Object>> list =  studentMapper.showall();
        System.out.println(list);
    }

//    class $Proxy0 implements StudentMapper


}
